<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://demo.thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\visitor\controller;

use app\company\service\DataService;
use library\Controller;
use think\Db;

/**
 * 访客来访记录管理
 * Class User
 * @package app\worker\controller
 */
class Record extends Controller
{

    /**
     * 绑定当前数据表
     * @var string
     */
    protected $table = 'VisitorList';

    /**
     * 来访记录(数据列表)
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index()
    {
		$this->title = '来访记录管理';

		$dbconf = $this->getconfig('vister_db');
        $uinfo = $this->getGuserinfo();

        $nickname = $this->request->param('nickname');
        $department = $this->request->param('department');
        $status = $this->request->param('status');
        $companyid = $this->request->param('companyid');
        $create_at_first=trim($this->request->param('create_at_first'))?strtotime($this->request->param('create_at_first')):'';
        $create_at_last=trim($this->request->param('create_at_last'))?strtotime($this->request->param('create_at_last'))+3600*24:'';


        switch ($uinfo['db1']['authorize']) {
            case '1':
                $where ='';
                # code...
                break;
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
                $where =' app_company_id = '.$uinfo['db2']['companyid'];
                break;
            case '8':
                $companyid = $uinfo['db1']['companyid']?$uinfo['db1']['companyid']:$uinfo['db2']['companyid'];
                $where ='property_id = '.$companyid;
                $res = Db::connect($dbconf)->table('property_relation')->where($where)->field('incompany_id')->select();
                if($res){
                    $companyid = array_column($res,"incompany_id");

                    $companyid = implode($companyid, ",");
                    $where = 'app_company_id in ('.$companyid.')';

                }else{

                    $where = 'app_company_id in ('."0".')';
                }
                # code...
                break;
            default:
                $this->error('无权限查看');
                # code...
                break;
        }

        if($this->request->param('user_id')){
            if($where){
                $where = $where.' and vister_uid = '.$this->request->param('user_id');
            }else{
                $where = 'vister_uid = '.$this->request->param('user_id');
            }

        }

        if(!$create_at_first||!$create_at_last){
        $this->_query(Db::connect($dbconf)->table('view_new_app_meeting_info_copy'))->where($where)
                                                                    ->where('appointment_uid is not null and vister_uid is not null and vister_arrivetime is not null')
                                                                    ->like('vister_name,app_company_name,app_address')
                                                                    ->equal('arrive_status')
                                                                    ->where('deleteTime is null')
                                                                    ->order('createtime desc')
                                                                    ->page();
        }else{
        $this->_query(Db::connect($dbconf)->table('view_new_app_meeting_info_copy'))->where($where)
                                                                    ->where('appointment_uid is not null and vister_uid is not null and vister_arrivetime is not null')
                                                                    ->like('vister_name,app_company_name,app_address')
                                                                    ->equal('arrive_status')
                                                                    ->where('deleteTime is null')
                                                                    ->where('app_visittime','between',[$create_at_first,$create_at_last])
                                                                    ->order('createtime desc')
                                                                    ->page();
        }
        //var_dump($where);


    }

    /**
     * 添加XX
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function add()
    {
        $this->_form($this->table, 'form');
    }

    /**
     * 修改XX
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function edit()
    {
        $this->_form($this->table, 'form');
    }

    /**
     * 权限表单数据处理
     * @param array $data 表单数据
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _form_filter(&$data)
    {
        if ($this->request->isGet()) {
            $where = ['status' => '1', 'is_deleted' => '0'];
            $this->auths = Db::name('company_user_auth')->where($where)->order('sort desc,id desc')->select();
            array_unshift($this->auths, ['id' => '0', 'title' => '所有权限', 'path' => '/']);
            $data['svn_authorize'] = isset($data['svn_authorize']) ? explode(',', $data['svn_authorize']) : [];
        } else {
            if (isset($data['svn_authorize']) && is_array($data['svn_authorize'])) {
                $data['svn_authorize'] = join(',', $data['svn_authorize']);
            } else {
                $data['svn_authorize'] = '';
            }
            // $macs = [];
            // foreach (explode(PHP_EOL, preg_replace("/\s+/", PHP_EOL, trim($data['mobile_macs']))) as $mac) {
            //     if (DataService::applyMacValue($mac)) $macs[] = $mac;
            // }
            // $data['mobile_macs'] = join(PHP_EOL, array_unique($macs));
        }
    }

    /**
     * 更改XX状态
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function state()
    {
        $this->_save($this->table, ['status' => input('status', '0')]);
    }

    /**
     * 删除XX
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function remove()
    {
        // $this->_delete($this->table);
		if($this->request->isPost()){      
		$id=trim($this->request->param('id'));
		// dump($id);
		$data=Db::name('VisitorList')->where("id in ($id)")->delete();
		if($data){
			$this->success('数据删除成功！');
		}
		}
    }

    /**
     * 来访详情
     * @auth         true
     * @throws           hink\Exception
     * @throws           hink\exception\PDOException
     * @HtttpRequest                              get|post
     * @Author       GNLEON
     * @Param
     * @DateTime     2020-10-19T11:56:24+0800
     * @LastTime     2020-10-19T11:56:24+0800
     * @return       [type]                       [description]
     */
    public function details(){
        $dbconf = $this->getconfig('vister_db');
        $uinfo = $this->getGuserinfo();

        $id = $this->request->param('id');
        $res = Db::connect($dbconf)->table('view_new_app_meeting_info')->where('id = '.$id)->find();

        $this->fetch('',['list'=>$res]);

    }

}
